namespace ContractAssertion
{
	using System;
	using System.Text;
	using System.Collections.Generic;
	using Microsoft.VisualStudio.TestTools.UnitTesting;
	using DependencyLib;
	using CrossFlow.GraphLib;
	using System.Xml;
	using System.IO;

	[TestClass]
	public class VisioDrawings
	{
		[TestMethod]
		public void NewDrawing()
		{
			IDependencyProvider provider = new MockDependencyProvider("digraph0");
			MockDependencyProvider depends = provider as MockDependencyProvider;
			depends.VertexNames.Add("v0");
			depends.VertexNames.Add("v1");
			depends.VertexNames.Add("v2");
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v0", "v1"));
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v0", "v2"));
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v1", "v2"));
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v2", "v1"));
			DirectedGraph graph = provider.CreateGraph();

			System.IO.StringWriter writer = new System.IO.StringWriter(); 
			DigraphAsVisioDrawingWriter visio = new DigraphAsVisioDrawingWriter();
			visio.Write(writer,graph);

			XmlDocument drawing = new XmlDocument();
			drawing.LoadXml(writer.GetStringBuilder().ToString());
			XmlNamespaceManager names = new XmlNamespaceManager(drawing.NameTable);
			names.AddNamespace("v", "http://schemas.microsoft.com/visio/2003/core");
			Assert.AreEqual<int>(7, drawing.SelectSingleNode("/v:VisioDocument/v:Pages/v:Page/v:Shapes",names).ChildNodes.Count);
			Assert.AreEqual<int>(8, drawing.SelectSingleNode("/v:VisioDocument/v:Pages/v:Page/v:Connects", names).ChildNodes.Count);
		}

		[TestMethod]
		public void SelfCircleDrawing()
		{
			IDependencyProvider provider = new MockDependencyProvider("digraph0");
			MockDependencyProvider depends = provider as MockDependencyProvider;
			depends.VertexNames.Add("v0");
			depends.VertexNames.Add("v1");
			depends.VertexNames.Add("v2");
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v0", "v1"));
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v0", "v2"));
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v1", "v2"));
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v2", "v2"));
			DirectedGraph graph = provider.CreateGraph();

			System.IO.StringWriter writer = new System.IO.StringWriter();
			DigraphAsVisioDrawingWriter visio = new DigraphAsVisioDrawingWriter();
			visio.Write(writer,graph);

			XmlDocument drawing = new XmlDocument();
			drawing.LoadXml(writer.GetStringBuilder().ToString());
			XmlNamespaceManager names = new XmlNamespaceManager(drawing.NameTable);
			names.AddNamespace("v", "http://schemas.microsoft.com/visio/2003/core");
			Assert.AreEqual<int>(7, drawing.SelectSingleNode("/v:VisioDocument/v:Pages/v:Page/v:Shapes", names).ChildNodes.Count);
			Assert.AreEqual<int>(8, drawing.SelectSingleNode("/v:VisioDocument/v:Pages/v:Page/v:Connects", names).ChildNodes.Count);
		}

		[TestMethod]
		public void UpdateExistingDrawing()
		{
			IDependencyProvider provider = new MockDependencyProvider("newone");
			MockDependencyProvider depends = provider as MockDependencyProvider;
			depends.VertexNames.Add("v0");
			depends.VertexNames.Add("v1");
			depends.VertexNames.Add("v2");
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v0", "v1"));
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v0", "v2"));
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v1", "v2"));
			DirectedGraph graph = provider.CreateGraph();

			StringWriter writer = new StringWriter();
			DigraphAsVisioDrawingWriter visio = new DigraphAsVisioDrawingWriter();
			visio.Write(writer,graph);

			provider = new MockDependencyProvider("dirty one");
			depends = provider as MockDependencyProvider;
			depends.VertexNames.Add("v3");
			depends.VertexNames.Add("v4");
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v0", "v3"));
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v2", "v4"));
			depends.EdgePairs.Add(new KeyValuePair<string, string>("v3", "v4"));
			GraphDelta delta = provider.AppendToGraph(graph);

			StringWriter new_drawing_writer = new StringWriter();
			visio.Append(writer.GetStringBuilder().ToString(), delta, new_drawing_writer);

			XmlDocument drawing = new XmlDocument();
			drawing.LoadXml(new_drawing_writer.GetStringBuilder().ToString());
			XmlNamespaceManager names = new XmlNamespaceManager(drawing.NameTable);
			names.AddNamespace("v", "http://schemas.microsoft.com/visio/2003/core");
			Assert.AreEqual<int>(11, drawing.SelectSingleNode("/v:VisioDocument/v:Pages/v:Page/v:Shapes", names).ChildNodes.Count);
			Assert.AreEqual<int>(12, drawing.SelectSingleNode("/v:VisioDocument/v:Pages/v:Page/v:Connects", names).ChildNodes.Count);
		}
	}
}